$(function() {
	var selfInfoBtn = $('#self-info'), 

		searchDiv = $('#search'),
		resultDiv = $('#result'),

		resumeListDiv = resultDiv.find('tbody');

		selfModal = $('#self-modal'),
		alert = selfModal.find('.alert'),

		resumeLink = $('#resume-link');

	var workageEnum = ['应届生', '1年', '2年', '3年', '4年', '5年', '5年以上'],
		degreEnum = ['大专', '本科', '硕士', '博士'],
		jobTypeEnum = ['全职','兼职','实习'];

	searchDiv.height($(window).height() - 250);
	resultDiv.height($(window).height() - 250);
	$(window).resize(function() {
	    searchDiv.height(Math.max($(this).height() - 250, 500));
	    resultDiv.height(Math.max($(this).height() - 250, 500));
	});

	selfInfoBtn.on('click', function() {
		alert.hide();
		$.get(MODELS.DOMAIN_PATH + '/company/id/' + MODELS.ID, function(result) {
			console.log(result);
			selfModal.find('input:eq(0)').val(result.name);
			selfModal.find('input:eq(1)').val(result.pwd);
			selfModal.find('input:eq(2)').val(result.address);
			selfModal.find('input:eq(3)').val(result.phone);
			selfModal.find('input:eq(4)').val(result.website);
		});
		selfModal.modal('show');
	});
	selfModal.find('.btn-primary').on('click', function() {
		alert.empty();

		var param = {
			name: selfModal.find('input:eq(0)').val(),
			pwd: selfModal.find('input:eq(1)').val(),
			address: selfModal.find('input:eq(2)').val(),
			phone: selfModal.find('input:eq(3)').val(),
			website: selfModal.find('input:eq(4)').val()
		};
		
        for (var key in param) {
            if (param[key].length == 0) {
            	alert.append('缺少' + key + ' 信息');
            	alert.show();
                return;
            }
        }
        $.post(MODELS.DOMAIN_PATH + '/company/id/' + MODELS.ID, param, function(result) {
        	if (result.succeeded) {
        		alert.append('Save successfully');
        		setTimeout(function() {
        			selfModal.modal('hide');
        		}, 500);
        	} else {
        		alert.append('Save failed');
        	}
        });
	});

	searchDiv.parent().find('button').on('click', function() {
		var param = {
			workage: getEnumVal( searchDiv.find('#workage').val() ),
			major: searchDiv.find('#major').val() || undefined,
			degree: getEnumVal( searchDiv.find('#degree').val() ),
			school: searchDiv.find('#school').val() || undefined,
			skill: searchDiv.find('#skill').val() || undefined,
			position: searchDiv.find('#position').val() || undefined,
			city: searchDiv.find('#city').val() || undefined,
			jobType: getEnumVal( searchDiv.find('#jobType').val() ),
		};

		$.get(MODELS.DOMAIN_PATH + '/seeker/some', param, function(list) {
			console.log(list);
			resumeListDiv.empty();
			list.forEach(function(resume) {
				resumeListDiv.append(constructResumeDiv(resume));
			});
		});
	});

	function constructResumeDiv(resume) {
		var div = $('<tr>' +
	                    '<td>' + workageEnum[resume.workage] + '</td>' +
	                    '<td>' + resume.major + '</td>' +
	                    '<td>' + degreEnum[resume.degree] + '</td>' +
	                    '<td>' + resume.school + '</td>' +
	                    '<td>' + (resume.skill || '') + '</td>' +
	                    '<td>' + (resume.position || '') + '</td>' +
	                    '<td>' + (resume.city || '') + '</td>' +
	                    '<td>' + (resume.jobType || '') + '</td>' +
	                    '<td>' + '<a href='+ MODELS.DOMAIN_PATH + '/seeker/' + resume.id  + '/template/1/pdf ' + 
                    			 'target="_blank" class="btn btn-primary btn-xs">简历</a>' + 
	                    '</td>' +
	                '</tr>');
		// div.find('button').on('click', function() {
		// 	resumeLink.attr('href', MODELS.DOMAIN_PATH + '/seeker/' + div.data('id') + '/template/1/pdf');
		// 	resumeLink.click();
		// });
		return div;
	}

	function getEnumVal(v) {
	    if (v && v != -1) {
	        return v;
	    } else {
	        return undefined;
	    }
	}
});